package Servlet.manager;

import Dao.UserDao;
import Manage.Camera;
import Manage.Manager;
import Manage.User;
import Manage.User_login;
import Utils.mybatisUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.io.PrintWriter;

public class caozuo1Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user=request.getParameter("user");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String sex=request.getParameter("sex");
        String caozuo=request.getParameter("caozuo");
        String phone=request.getParameter("phone");
        String address=request.getParameter("address");
        String num=request.getParameter("num");
        String iddd=request.getParameter("iddd");
        String street=request.getParameter("street");
        String idd=request.getParameter("idd");
        String keyword=request.getParameter("keyword");
        PrintWriter pw =response.getWriter();
        SqlSession session= mybatisUtils.getSqlSession();
        UserDao dao=session.getMapper(UserDao.class);
        if ("改".equals(caozuo)) {
            //用户已经修改了信息
            if (!"null".equals(idd))
            {
                User user1 = new User();
                user1.setNum(Integer.parseInt(num));
                user1.setSex(sex);
                user1.setPhone(phone);
                user1.setAddress(address);
                user1.setUsername(username);
                user1.setIdd(Integer.parseInt(idd));
                //管理者对用户信息更改
                dao.manageupdate(user1);
            }
            //用户没有修改信息
            else {
                User user1 = new User();
                user1.setNum(Integer.parseInt(num));
                user1.setSex(sex);
                user1.setPhone(phone);
                user1.setAddress(address);
                user1.setUsername(username);
                //管理者插入用户信息
                dao.usersinsert(user1);
            }
            User_login user_login=new User_login();
            user_login.setPassword(password);
            user_login.setUsername(username);
            user_login.setIddd(Integer.parseInt(iddd));
            Camera camera=new Camera();
            camera.setStreet(street);
            camera.setUsername(username);
            //当前有该摄像头信息，跟新,因为keyword接收到的为字符串，所以是字符null
            if (!"null".equals(keyword)){
                camera.setKeyword(Integer.parseInt(keyword));
                //管理者对用户摄像头信息更改
                dao.cameraupda(camera);
            }
            //当前没有该摄像头信息，插入
            else{
                dao.caminsert(camera);
            }
            //对登录信息进行更改
            dao.loginupdate(user_login);
            session.commit();
            pw.print("操作成功");
        }
        else {
            if ("删".equals(caozuo)){
                //管理者对用户摄像头信息删除
                if (!"null".equals(keyword))
                    dao.cameradel(Integer.parseInt(keyword),null);
            }
            else if ("删用户".equals(caozuo)){
                //管理者对用户信息和摄像头信息删除
                dao.usersdel(username);
                dao.cameradel(null,username);
            }
            session.commit();
            pw.print("操作成功");
        }
        pw.flush();
        pw.close();
        session.close();
    }
}
